#region Copyright (c) 2003, newtelligence AG. All rights reserved.
/*
// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
// Original BlogX Source Code: Copyright (c) 2003, Chris Anderson (http://simplegeek.com)
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without modification, are permitted 
// provided that the following conditions are met: 
//  
// (1) Redistributions of source code must retain the above copyright notice, this list of 
// conditions and the following disclaimer. 
// (2) Redistributions in binary form must reproduce the above copyright notice, this list of 
// conditions and the following disclaimer in the documentation and/or other materials 
// provided with the distribution. 
// (3) Neither the name of the newtelligence AG nor the names of its contributors may be used 
// to endorse or promote products derived from this software without specific prior 
// written permission.
//      
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -------------------------------------------------------------------------
//
// Original BlogX source code (c) 2003 by Chris Anderson (http://simplegeek.com)
// 
// newtelligence is a registered trademark of newtelligence Aktiengesellschaft.
// 
// For portions of this software, the some additional copyright notices may apply 
// which can either be found in the license.txt file included in the source distribution
// or following this notice. 
//
*/
#endregion

using System;
using System.Xml;
using System.Configuration;
using System.IO;
using System.Collections;
using System.Reflection;

namespace newtelligence.DasBlog.Web.Core
{
	/// <summary>
	/// Internal configuration class
	/// </summary>
	public class ThemeSectionHandler : IConfigurationSectionHandler
	{
		/*
		 * <newtelligence.Weblog.Themes>
		 *     <theme name="discreetBlogBlue" 
		 *            title="Discreet Blog Blue"
		 *            templateDirectory="themes/discreetBlogBlue"
		 *            imageDirectory="" />
		 * </newtelligence.weblog.Themes>
		 */
		object IConfigurationSectionHandler.Create(object parent, object context, XmlNode sectionRoot )
		{
			ThemeDictionary configData = new ThemeDictionary();

			XmlNodeList ndNodeList = ((XmlElement)sectionRoot).GetElementsByTagName("theme");
			foreach( XmlElement elTheme in ndNodeList  )
			{
				Theme theme = new Theme();
				theme.Name = elTheme.GetAttribute("name");
				theme.Title = elTheme.GetAttribute("title");
				theme.TemplateDirectory = elTheme.GetAttribute("templateDirectory");
				theme.ImageDirectory = elTheme.GetAttribute("imageDirectory");
                foreach( XmlElement elImage in elTheme.GetElementsByTagName("image")  )
                {
                    string name = elImage.GetAttribute("name");
                    string fileName = elImage.GetAttribute("fileName");
                    if ( name != null && fileName != null )
                    {
                        theme.ImageList.Add( name, fileName );
                    }
                }
				configData.Add( theme.Name, theme );
			}
			return configData;
		}
	}

	public class Theme
	{
		public const string DefaultTheme = "default";

        private Hashtable imageList = new Hashtable();
		private string name;
		private string title;
		private string templateDirectory;
		private string imageDirectory;
		
		public string Name 
		{
			get
			{
				return name;
			}
			set
			{
				name = value;
			}
		}

		public string Title 
		{
			get
			{
				return title;
			}
			set
			{
				title = value;
			}
		}

		public string TemplateDirectory 
		{
			get
			{
				return templateDirectory;
			}
			set
			{
				templateDirectory = value;
			}
		}

		public string ImageDirectory 
		{
			get
			{
				return imageDirectory;
			}
			set
			{
				imageDirectory = value;
			}
		}

        public Hashtable ImageList
        {
            get
            {
                return imageList;
            }
        }


        protected TextReader OpenTemplate( string templateName, string basePath, string categoryName)
        {
            string radioStyleTemplate = "#"+templateName+".txt";
            string dasBlogStyleTemplate = templateName+".blogtemplate";
            string htmlStyleTemplate = templateName+".html";

            string sanitizedCategoryName = categoryName.Replace("|","\\");
            sanitizedCategoryName = sanitizedCategoryName.Replace(".","");

            // try blogStyle with category name first
            string filePath = Path.Combine(Path.Combine(Path.Combine(basePath,TemplateDirectory),sanitizedCategoryName),dasBlogStyleTemplate);
            if ( File.Exists( filePath ) )
            {
                return new StreamReader( filePath,  true );
            }
            else 
            {
                // now without category name
                filePath = Path.Combine(Path.Combine(basePath,TemplateDirectory),dasBlogStyleTemplate);
                if ( File.Exists( filePath ) )
                {
                    return new StreamReader( filePath,  true );
                }
            }

            // try html with category name now
            filePath = Path.Combine(Path.Combine(Path.Combine(basePath,TemplateDirectory),sanitizedCategoryName),htmlStyleTemplate);
            if ( File.Exists( filePath ) )
            {
                return new StreamReader( filePath,  true );
            }
            else 
            {
                // now without category name
                filePath = Path.Combine(Path.Combine(basePath,TemplateDirectory),htmlStyleTemplate);
                if ( File.Exists( filePath ) )
                {
                    return new StreamReader( filePath,  true );
                }
            }

            // now the same for radio templates
            filePath = Path.Combine(Path.Combine(Path.Combine(basePath,TemplateDirectory),sanitizedCategoryName),radioStyleTemplate);
            if ( File.Exists( filePath ) )
            {
                return new StreamReader( filePath,  true );
            }
            else 
            {
                // now without category name
                filePath = Path.Combine(Path.Combine(basePath,TemplateDirectory),radioStyleTemplate);
                if ( File.Exists( filePath ) )
                {
                    return new StreamReader( filePath,  true );
                }
            }
            return null;
        }

        protected TextReader OpenEmptyTemplate()
        {
            return new StreamReader("<p>no template</p>");
        }

		public TextReader OpenMainTemplate(string basePath, string categoryName)
		{
            TextReader templateReader = OpenTemplate("template", basePath, categoryName );
            if ( templateReader == null )
            {
                templateReader = OpenEmptyTemplate();
            }
			return templateReader;
		}

        public TextReader OpenHomeTemplate(string basePath, string categoryName)
		{
			TextReader templateReader = OpenTemplate("homeTemplate", basePath, categoryName );
            if ( templateReader == null )
            {
                templateReader = OpenEmptyTemplate();
            }
            return templateReader;
		}

		public TextReader OpenDesktopTemplate(string basePath, string categoryName)
		{
			TextReader templateReader = OpenTemplate("desktopWebsiteTemplate", basePath, categoryName );
            if ( templateReader == null )
            {
                templateReader = OpenEmptyTemplate();
            }
            return templateReader;
		}

		public TextReader OpenItemTemplate(string basePath, string categoryName)
		{
			TextReader templateReader = OpenTemplate("itemTemplate", basePath, categoryName );
            if ( templateReader == null )
            {
                templateReader = OpenEmptyTemplate();
            }
            return templateReader;
		}

		public TextReader OpenDayTemplate(string basePath, string categoryName)
		{
			TextReader templateReader = OpenTemplate("dayTemplate", basePath, categoryName );
            if ( templateReader == null )
            {
                templateReader = OpenEmptyTemplate();
            }
            return templateReader;
		}
        
	}

	/// <summary>
	/// A dictionary with keys of type string and values of type Theme
	/// </summary>
	public class ThemeDictionary: System.Collections.DictionaryBase
	{
		/// <summary>
		/// Initializes a new empty instance of the ThemeDictionary class
		/// </summary>
		public ThemeDictionary()
		{
			// empty
		}

		/// <summary>
		/// Gets or sets the Theme associated with the given string
		/// </summary>
		/// <param name="key">
		/// The string whose value to get or set.
		/// </param>
		public virtual Theme this[string key]
		{
			get
			{
				return (Theme) this.Dictionary[key];
			}
			set
			{
				this.Dictionary[key] = value;
			}
		}

		/// <summary>
		/// Adds an element with the specified key and value to this ThemeDictionary.
		/// </summary>
		/// <param name="key">
		/// The string key of the element to add.
		/// </param>
		/// <param name="value">
		/// The Theme value of the element to add.
		/// </param>
		public virtual void Add(string key, Theme value)
		{
			this.Dictionary.Add(key, value);
		}

		/// <summary>
		/// Determines whether this ThemeDictionary contains a specific key.
		/// </summary>
		/// <param name="key">
		/// The string key to locate in this ThemeDictionary.
		/// </param>
		/// <returns>
		/// true if this ThemeDictionary contains an element with the specified key;
		/// otherwise, false.
		/// </returns>
		public virtual bool Contains(string key)
		{
			return this.Dictionary.Contains(key);
		}

		/// <summary>
		/// Determines whether this ThemeDictionary contains a specific key.
		/// </summary>
		/// <param name="key">
		/// The string key to locate in this ThemeDictionary.
		/// </param>
		/// <returns>
		/// true if this ThemeDictionary contains an element with the specified key;
		/// otherwise, false.
		/// </returns>
		public virtual bool ContainsKey(string key)
		{
			return this.Dictionary.Contains(key);
		}

		/// <summary>
		/// Determines whether this ThemeDictionary contains a specific value.
		/// </summary>
		/// <param name="value">
		/// The Theme value to locate in this ThemeDictionary.
		/// </param>
		/// <returns>
		/// true if this ThemeDictionary contains an element with the specified value;
		/// otherwise, false.
		/// </returns>
		public virtual bool ContainsValue(Theme value)
		{
			foreach (Theme item in this.Dictionary.Values)
			{
				if (item == value)
					return true;
			}
			return false;
		}

		/// <summary>
		/// Removes the element with the specified key from this ThemeDictionary.
		/// </summary>
		/// <param name="key">
		/// The string key of the element to remove.
		/// </param>
		public virtual void Remove(string key)
		{
			this.Dictionary.Remove(key);
		}

		/// <summary>
		/// Gets a collection containing the keys in this ThemeDictionary.
		/// </summary>
		public virtual System.Collections.ICollection Keys
		{
			get
			{
				return this.Dictionary.Keys;
			}
		}

		/// <summary>
		/// Gets a collection containing the values in this ThemeDictionary.
		/// </summary>
		public virtual System.Collections.ICollection Values
		{
			get
			{
				return this.Dictionary.Values;
			}
		}
	}
}
